/* ****************************************************************************
 *
 * Copyright (c) Microsoft Corporation. 
 *
 * This source code is subject to terms and conditions of the Microsoft Public License. A 
 * copy of the license can be found in the License.html file at the root of this distribution. If 
 * you cannot locate the  Microsoft Public License, please send an email to 
 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 *
 * You must not remove this notice, or any other, from this software.
 *
 * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
 * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
 * PARTICULAR PURPOSE.
 * ***************************************************************************/


#include "stdafx.h"
#include <pgl/core/PGLData.h>
#include <pgl/core/DataRegistry.h>

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

namespace pgl
{

CDataRegistry::CDataRegistry() 
{
	PGL_TRACE(_T("Data registry starting\n"));
};

CDataRegistry::~CDataRegistry()
{
	PGL_TRACE(_T("Data registry closing\n"));
}

void CDataRegistry::RegisterDataP(LPCTSTR szName, CPGLDataPtr (__cdecl* fcn)(void))
{
	PGL_TRACE1(_T("Registering %s in Data Object register "),szName);
	if ( m_registry.find(szName) == m_registry.end())
	{
		m_registry[szName]=fcn;
		PGL_TRACE1(_T("...Adding to register (%u objects)"), m_registry.size());
	}
	PGL_TRACE(_T("...Done\n"));	
}

CPGLDataPtr CDataRegistry::GetDataP(LPCTSTR szName)
{
	PGL_TRACE1(_T("CDataRegistry: looking for %s "), szName);
	CRegistry::iterator i=m_registry.find(szName);
	if ( i == m_registry.end())
	{
		PGL_TRACE(_T("... NOT found\n"));
		return CPGLDataPtr();
	}
	else
	{
		PGL_TRACE(_T("... found\n"));
		return i->second();
	}
}

size_t CDataRegistrySingleton::GetCount()
{
	return Instance()->GetCountP();
}

void CDataRegistrySingleton::RegisterData(LPCTSTR szName, CPGLDataPtr (__cdecl *fcn)(void) )
{
	Instance()->RegisterDataP(szName,fcn);
}

CPGLDataPtr CDataRegistrySingleton::GetData(LPCTSTR szName)
{
	return Instance()->GetDataP(szName);
}

CDataRegisterer::CDataRegisterer(LPCTSTR szName, CPGLDataPtr (*fcn)(void))
{	
	CDataRegistrySingleton::RegisterData( szName, fcn); 
};

};
